<?php

namespace App\Http\Controllers\Home;

use App\Model\Address;
use App\Models\User;
use App\Models\Users;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class AddressController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        //
        $value = $request->session()->get('user');

        $uid = $value['uid'];

        $address = Users::find($uid)->address()->get();

        return view('home/address',['address'=>$address,'uid'=>$uid]);

    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
        $temp = $request -> except('_token');
        $temp['baddress'] = $temp["province"].','.$temp["country"].','.$temp["town"].','.$temp['baddress'];
        $data['b_uid'] = $temp['buid'];
        $data['bconsignee'] = $temp['bconsignee'];
        $data['bphone'] = $temp['bphone'];
        $data['baddress'] = $temp['baddress'];
        $data['bpostalcode'] = $temp['bpostalcode'];
        $data['bdate'] = time();
        $res = Address::create($data);
        if($res){
            return redirect('/home/address')->with('success','添加成功');
        }else{
            return back()->with('error','添加失败');
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($bid)
    {
        //
        $data = Address::find($bid)->get();
        $baddress  =$data[0]['baddress'];

        $info = explode(',',$baddress);
        //dd($info);
        $data['province'] = $info[0];
        $data['country'] = $info[1];
        $data['town'] = $info[2];
        $data['baddress'] = $info[3];

      return view('home/addressEdit',['data'=>$data]);

    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //

        $temp = $request -> except('_token');
        $temp['baddress'] = $temp["province"].','.$temp["country"].','.$temp["town"].','.$temp['baddress'];
        $data['b_uid'] = $temp['buid'];
        $data['bconsignee'] = $temp['bconsignee'];
        $data['bphone'] = $temp['bphone'];
        $data['baddress'] = $temp['baddress'];
        $data['bpostalcode'] = $temp['bpostalcode'];
        $data['bdate'] = time();
        $res = Address::find($id)->update($data);
        if($res){
            return redirect('/home/address')->with('success','修改成功');
        }else{
            return back()->with('error','修改失败');
        }

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function del(Request $request)
    {
            $a = $request->all();
            $bid =$a['bid'];
           $res = Address::find($bid)->delete();
           if($res){
               return 1;
           }else{
               return 0;
           }
          //return json_encode ($bid);


    }
}
